package com.haojiang.mh.remote.ui.dialog

import android.os.Bundle
import android.text.TextUtils
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.core.os.bundleOf
import androidx.fragment.app.FragmentActivity
import com.haojiang.mh.AppContext
import com.haojiang.mh.R
import com.haojiang.mh.remote.base.BaseDialogFrag
import com.haojiang.mh.remote.enums.EConnect
import com.haojiang.mh.remote.event.AddGroupEvent
import com.haojiang.mh.remote.utils.BleUtil.Companion.addGroupKey
import kotlinx.android.synthetic.main.frag_dialog_group_name.*
import org.greenrobot.eventbus.EventBus


class GroupNameDialogFrag : BaseDialogFrag() {

    /**
     * 伴生函数
     */
    companion object {
        private val GROUP_TYPE = "group_type"

        val instance by lazy {
            GroupNameDialogFrag()
        }
//        private var instance: GroupNameDialogFrag? = null
//            get() {
//                if (field == null) {
//                    field = GroupNameDialogFrag()
//                }
//                return field
//            }
//
//        private fun get(): GroupNameDialogFrag {
//            return instance!!
//        }


        fun show(fragmentActivity: FragmentActivity, groupType: Int) {
            val fm = fragmentActivity.supportFragmentManager
            fm.executePendingTransactions()
            fm.beginTransaction().remove(instance)
            instance.arguments = bundleOf(GROUP_TYPE to groupType)
            instance.show(fm, GroupNameDialogFrag::class.java.simpleName)

//            if (get().dialog != null && !get().dialog?.isShowing!!) {
//                get().dialog?.show()
//            } else {
//                get().arguments = bundleOf(BUNDLE_GROUP_TYPE to groupType)
//                get().show(fragmentActivity.supportFragmentManager, BleDialogFrag::class.java.name)
//            }
        }
    }


    private var groupType = EConnect.CONNECT_GROUP_SINGLE.data   //标识当前选择的组类型 单一，多个，分离
    private val onClickListener = object : View.OnClickListener {
        override fun onClick(v: View?) {
            if (v != null) {
                when (v.id) {
                    R.id.tvConfirm -> {
                        val groupName = etGroupName.text.toString().trim()
                        if (TextUtils.isEmpty(groupName)) {
                            showToast(
                                AppContext.instance()
                                    .getString(R.string.m_content_can_not_be_empty)
                            )
                            return
                        }

                        val groupId = addGroupKey(context!!, groupName, groupType)
                        if (groupId != 0L) {
                            dismiss()
//                            EventBus.getDefault().post(AddGroupEvent(groupId))
//                            BleDialogFrag.show(activity!!, groupType, groupId)
                            EventBus.getDefault()
                                .post(AddGroupEvent())                                                 //更新组列表
                            BleGroupDialogFrag.show(
                                activity!!,
                                groupType,
                                groupId
                            )                              //显示并搜索蓝牙子选项
                        } else {
//                            showToast()
                        }


//                        val groupId = SimpleDateFormat("yyyy_MM_dd_HH_mm_ss").format(Date())
//                        val groupKey = GroupKey(groupId, groupName, groupType)
////                        val groupValueList = ArrayList<GroupValue>()
////                        groupValueList.add(GroupValue("A8-1E-84-B0-A1-65", "设备1"))
////                        groupValueList.add(GroupValue("A8-1E-84-B0-A1-66", "设备2"))
//                        val bleGroup = BleGroup(groupKey, ArrayList<GroupValue>())
//                        groupMap.put(groupId, bleGroup)
//                        if (SpUtil.putHashMapData(context, KEY_GROUP, groupMap)) {
//                            context?.getString(R.string.m_save_success)?.let { showToast(it) }
//                        }
                    }
                    R.id.ivClose -> {
                        dismiss()
                    }
                }
            }
        }
    }

    override fun onSubCreate(savedInstanceState: Bundle?) {
        setStyle(STYLE_NO_TITLE, R.style.fragment_dialog)
        groupType = arguments?.get(GROUP_TYPE) as Int
    }

    override fun onSubCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.frag_dialog_group_name, container, false)
    }

    override fun onSubViewCreated(view: View, savedInstanceState: Bundle?) {
        dialog!!.setCancelable(true) //是否可以点击取消
        dialog!!.setCanceledOnTouchOutside(false) //dialog外面是否可以点击

        val params = dialog!!.window!!.attributes
        val context = context
        context?.let {
            //            params.width = (Util.getScreenWidth(context) * 0.7f).toInt()
            params.width = LinearLayout.LayoutParams.WRAP_CONTENT
            params.height = LinearLayout.LayoutParams.WRAP_CONTENT
            params.gravity = Gravity.CENTER
            //高度自己定义
            dialog!!.window!!.setLayout(params.width, params.height)
        }
    }


    override fun onSubActivityCreated(savedInstanceState: Bundle?) {
        tvConfirm.setOnClickListener(onClickListener)
        ivClose.setOnClickListener(onClickListener)
    }

    override fun initDestroy() {
    }


}